#ifndef __QUANTITY_FORMATOR_H__
#define __QUANTITY_FORMATOR_H__
#include <iostream>
using namespace std;

#define FormatorInStdFormat(Unit) QuantityFormatorInStdFormat<Unit>
#define FormatorInBaseUnit(Unit)  QuantityFormatorInBaseUnit<Unit>

template <class T> 
ostream& QuantityFormatorInStdFormat(ostream& out, unsigned long amount, const T &unit)
{
    unsigned long leftNumber = amount * unit.factor();
    T tempUnit = unit.topUnit();
    while (1)
    {
        out << leftNumber / tempUnit.factor() << " " << tempUnit;
        if (tempUnit == unit.baseUnit())
        {
            return out;
        }
        leftNumber %= tempUnit.factor();
        --tempUnit;
        out << " ";
    }
}

template <class T> 
ostream& QuantityFormatorInBaseUnit(ostream& out, unsigned long amount, const T &unit)
{
    unsigned long number = amount * unit.factor() / unit.baseUnit().factor();
    return out << number << " " << unit.baseUnit();
}

#endif


